/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.watchmen.mini.modules.sys.utils;

import java.util.List;

import com.watchmen.mini.common.service.BaseService;
import com.watchmen.mini.common.utils.SpringContextHolder;
import com.watchmen.mini.modules.sys.dao.AreaDao;
import com.watchmen.mini.modules.sys.dao.MenuDao;
import com.watchmen.mini.modules.sys.dao.OfficeDao;
import com.watchmen.mini.modules.sys.dao.RoleDao;
import com.watchmen.mini.modules.sys.dao.UserDao;
import com.watchmen.mini.modules.sys.entity.Area;
import com.watchmen.mini.modules.sys.entity.Menu;
import com.watchmen.mini.modules.sys.entity.Office;
import com.watchmen.mini.modules.sys.entity.Role;
import com.watchmen.mini.modules.sys.entity.User;

/**
 * 用户工具类
 * @author ThinkGem
 * @version 2013-12-05
 */
public class UserUtils {

	private static UserDao userDao = SpringContextHolder.getBean(UserDao.class);
	private static RoleDao roleDao = SpringContextHolder.getBean(RoleDao.class);
	private static MenuDao menuDao = SpringContextHolder.getBean(MenuDao.class);
	private static AreaDao areaDao = SpringContextHolder.getBean(AreaDao.class);
	private static OfficeDao officeDao = SpringContextHolder.getBean(OfficeDao.class);
    public static final ThreadLocal<User> env = new ThreadLocal<User>();



	/**
	 * 根据ID获取用户
	 * @param id
	 * @return 取不到返回null
	 */
	public static User get(String id){
		User user =  userDao.get(id);
        if (user == null){
            return null;
        }
        user.setRoleList(roleDao.findList(new Role(user)));
		return user;
	}
	
	/**
	 * 根据登录名获取用户
	 * @param loginName
	 * @return 取不到返回null
	 */
	public static User getByLoginName(String loginName){
		User user  =null;
        user = userDao.getByLoginName(new User(null, loginName));
        if (user == null){
            return null;
        }
        user.setRoleList(roleDao.findList(new Role(user)));
		return user;
	}
	

    /**
     * 获取当前用户
     *
     * @return 取不到返回 new User()
     */
    public static User getUser() {
        return env.get();
    }

    public static void setUser(User user) {
        env.set(user);
    }

	/**
	 * 获取当前用户角色列表
	 * @return
	 */
	public static List<Role> getRoleList(){
		List<Role> roleList = null;
        User user = getUser();
        if (user.isAdmin()){
            roleList = roleDao.findAllList(new Role());
        }else{
            Role role = new Role();
            role.getSqlMap().put("dsf", BaseService.dataScopeFilter(user.getCurrentUser(), "o", "u"));
            roleList = roleDao.findList(role);
        }
		return roleList;
	}
	
	/**
	 * 获取当前用户授权菜单
	 * @return
	 */
	public static List<Menu> getMenuList(){
		@SuppressWarnings("unchecked")
		List<Menu> menuList = null;
        User user = getUser();
        if (user.isAdmin()){
            menuList = menuDao.findAllList(new Menu());
        }else{
            Menu m = new Menu();
            m.setUserId(user.getId());
            menuList = menuDao.findByUserId(m);
        }
		return menuList;
	}
	
	/**
	 * 获取当前用户授权的区域
	 * @return
	 */
	public static List<Area> getAreaList(){
		List<Area> areaList = areaDao.findAllList(new Area());
		return areaList;
	}
	
	/**
	 * 获取当前用户有权限访问的部门
	 * @return
	 */
	public static List<Office> getOfficeList(){
		List<Office> officeList = null;
        User user = getUser();
        if (user.isAdmin()){
            officeList = officeDao.findAllList(new Office());
        }else{
            Office office = new Office();
            office.getSqlMap().put("dsf", BaseService.dataScopeFilter(user, "a", ""));
            officeList = officeDao.findList(office);
        }
		return officeList;
	}

	/**
	 * 获取当前用户有权限访问的部门
	 * @return
	 */
	public static List<Office> getOfficeAllList(){
		List<Office> officeList = officeDao.findAllList(new Office());;
		return officeList;
	}
	
}
